package temporay.worker;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;

/**
 * program: io-share 
 * author: bsworld.xie 
 * create: 2021-04-20 15:34 
 * description: 
 */
public class WorkerService {

    private ExecutorService workers;


    public WorkerService(int numThreads) {
        workers = Executors.newFixedThreadPool(numThreads);
    }

    public void doWork(Runnable workRequest) {

        if (workers != null) {
            try {
                //fixme 在业务线程里进行业务处理
                workers.execute(workRequest);
            } catch (RejectedExecutionException e) {

            }
        } else {
            //fixme 在IO线程里进行业务处理，适用于快速处理完成的业务
            workRequest.run();
        }
    }

}
